﻿using Appraisal_System.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Appraisal_System.Models
{
    public class Users
    {
        public int Id { get; set; }
        public string UserName { get; set; }
        public string PassWord { get; set; }
        public string Sex { get; set; }
        public int BaseTypeId { get; set; }
        public bool IsDel { get; set; }
        public static List<Users> ListAll()
        {
            List<Users> users = new List<Users>();
            DataTable dt = SqlHelper.ExecuteTable("select u.id,u.UserName,u.PassWord,u.Sex,u.BaseTypeId,u.IsDel from Users u;");
            foreach (DataRow dr in dt.Rows)
            {
                users.Add(dr.DataRowToModel<Users>());
            }
            return users;
        }
        public static int Insert(Users user)
        {
            return SqlHelper.ExecuteNonQuery($"insert into Users(UserName,PassWord,Sex,BaseTypeId,IsDel) values(@UserName,@PassWord,@Sex,@BaseTypeId,@IsDel)",
                  new SqlParameter("@UserName", user.UserName),
                  new SqlParameter("@PassWord", user.PassWord),
                  new SqlParameter("@Sex", user.Sex),
                  new SqlParameter("@BaseTypeId", user.BaseTypeId),
                  new SqlParameter("@IsDel", user.IsDel)
                  );
        }
        public static int Update(Users user)
        {
            return SqlHelper.ExecuteNonQuery($"update Users set UserName=@UserName,PassWord=@PassWord,Sex=@Sex,BaseTypeId=@BaseTypeId,IsDel=@IsDel where Id=@Id",
                  new SqlParameter("@UserName", user.UserName),
                  new SqlParameter("@PassWord", user.PassWord),
                  new SqlParameter("@Sex", user.Sex),
                  new SqlParameter("@BaseTypeId", user.BaseTypeId),
                  new SqlParameter("@IsDel", user.IsDel),
                  new SqlParameter("@Id", user.Id)
                  );
        }
    }
}
